/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
/********************************************************************\
                            Includes
\********************************************************************/
#include <QObject>
#include <QDateTime>
#include "htrsinternalsession.h"
#include "htrsmap.h"
#include "htrsmimedataresource.h"
#include "htr/mansock.h"

/********************************************************************\
                          Implementation
\********************************************************************/
HtrsInternalSession::HtrsInternalSession(HtrsMap *resources)
 : QObject()
{
  mResources        = resources;
  mStartDateTime    = QDateTime::currentDateTime();

  connect(mResources, SIGNAL(changesPerSecond(int)),
          this, SLOT(updateChangesPerSecond(int)));
 
  // Datetime-Setup: 
  bool created;
  HtrsResource *res = mResources->access("htree/server/datetime",Htr::MimeDataType, created);
  mDateTimeResource = dynamic_cast<HtrsMimeDataResource*>(res); Q_ASSERT(mDateTimeResource);

  mDateTimeResource->setPermanent(true);
  mDateTimeResource->open(Htr::Res::Create | Htr::Res::WriteOnly | Htr::Res::WriteExclusive);
  mDateTimeResource->setMimeType("DataStream/DateTime");

  // UpTime-Setup
  res = mResources->access("htree/server/uptime",Htr::MimeDataType, created);
  mUptimeResource = dynamic_cast<HtrsMimeDataResource*>(res); Q_ASSERT(mUptimeResource);

  mUptimeResource->setPermanent(true);
  mUptimeResource->open(Htr::Res::Create | Htr::Res::WriteOnly | Htr::Res::WriteExclusive);
  mUptimeResource->setMimeType("DataStream/Int64");

  updateSystemTime();

  connect(&mDateTimeTimer, SIGNAL(timeout()),
          this, SLOT(updateSystemTime()));
  mDateTimeTimer.start(900);  // mindestens 1x pro Sekunde..

  // Server-Version-Setup
  res = mResources->access("htree/server/version",Htr::MimeDataType, created);
  mVersionResource = dynamic_cast<HtrsMimeDataResource*>(res); Q_ASSERT(mDateTimeResource);

  mVersionResource->setPermanent(true);
  mVersionResource->open(Htr::Res::Create | Htr::Res::WriteOnly | Htr::Res::WriteExclusive);
  mVersionResource->setMimeType("text/plain");
  mVersionResource->setMimeData(QString("0.3.0").toAscii());
  
  // Session-Count-Setup
  res = mResources->access("htree/server/sessioncount",Htr::MimeDataType, created);
  mSessionCountResource = dynamic_cast<HtrsMimeDataResource*>(res); Q_ASSERT(mSessionCountResource);

  mSessionCountResource->setPermanent(true);
  mSessionCountResource->open(Htr::Res::Create | Htr::Res::WriteOnly | Htr::Res::WriteExclusive);
  mSessionCountResource->setMimeType("DataStream/Int32");

  // Session-Count-Setup
  res = mResources->access("htree/server/changespersecond",Htr::MimeDataType, created);
  mCPSResource = dynamic_cast<HtrsMimeDataResource*>(res); Q_ASSERT(mCPSResource);

  mCPSResource->setPermanent(true);
  mCPSResource->open(Htr::Res::Create | Htr::Res::WriteOnly | Htr::Res::WriteExclusive);
  mCPSResource->setMimeType("DataStream/Int32");
}

HtrsInternalSession::~HtrsInternalSession()
{
}

void HtrsInternalSession::updateSystemTime()
{
  Q_ASSERT(mDateTimeResource);
  QByteArray  dateTimePaket;
  QDataStream outStream(&dateTimePaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream);
  outStream << QDateTime::currentDateTime();
  mDateTimeResource->setMimeData(dateTimePaket);

  Q_ASSERT(mUptimeResource);
  QByteArray  upTimePaket;
  QDataStream outStream2(&upTimePaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream2);
  outStream2 << (qint64)(mStartDateTime.secsTo(QDateTime::currentDateTime()));
  mUptimeResource->setMimeData(upTimePaket);
}

void HtrsInternalSession::updateSessionCount(int sessionCount)
{
  Q_ASSERT(mSessionCountResource);
  QByteArray  dateTimePaket;
  QDataStream outStream(&dateTimePaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream);
  outStream << (qint32)sessionCount;
  mSessionCountResource->setMimeData(dateTimePaket);
}

void HtrsInternalSession::updateChangesPerSecond(int count)
{
  Q_ASSERT(mSessionCountResource);
  QByteArray  dateTimePaket;
  QDataStream outStream(&dateTimePaket,QIODevice::WriteOnly);
  HtrManagedSocket::setupStream(outStream);
  outStream << (qint32)count;
  mCPSResource->setMimeData(dateTimePaket);
}

